//
//  PSWageRangePickerView.m
//  Btpin
//
//  Created by Vic on 2019/6/16.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "PSWageRangePickerView.h"

@interface PSWageRangePickerView () <UIPickerViewDelegate, UIPickerViewDataSource>
{
    NSInteger leftIndex;
    NSInteger rightIndex;
}
@property (strong, nonatomic) NSMutableArray *itemDataArray;
@property (strong, nonatomic) UIPickerView *pickView;
@end

@implementation PSWageRangePickerView

- (instancetype)initWithDataArray:(NSArray *)dataArray target:(id)target {
    self = [super init];
    if (self) {
        self.itemDataArray = [NSMutableArray arrayWithCapacity:0];
        [self.itemDataArray addObjectsFromArray:dataArray];
        
        leftIndex = 0;
        rightIndex = 0;
        [self initUI];
        self.delegate = target;
    }
    return self;
}
- (void)initUI {
    self.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
    self.backgroundColor = [UIColor colorWithWhite:0.01 alpha:0.5];
    [self addTarget:self action:@selector(cancelButtonTap) forControlEvents:UIControlEventTouchUpInside];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    UIView *bgView = [UIToolsControl initViewWithFrame:CGRectMake(0, HEIGHT-SIMEmojKeyBoardkBottomNOSafeArea_Height-PickerHeight-50*Scale, WIDTH, PickerHeight+50*Scale) backgroundColor:COLOR_OF_WHITE];
    [self addSubview:bgView];
    bgView.userInteractionEnabled = YES;
    
    self.pickView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, PickerHeight)];
    self.pickView.delegate = self;
    self.pickView.dataSource = self;
    self.pickView.backgroundColor = COLOR_OF_WHITE;
    [bgView addSubview:self.pickView];
    
    UIButton *cancelBtn = [UIToolsControl initButtonWithFrame:CGRectMake(0, ViewHeight(bgView)-50*Scale, WIDTH*0.5, 50*Scale) imageName:nil target:self action:@selector(cancelButtonTap) title:CancelText titleColor:THEMECOLOR_OF_TEXT_9a9a9a];
    cancelBtn.titleLabel.font = Font(Font_17);
    [bgView addSubview:cancelBtn];
    [cancelBtn borderColor:THEMECOLOR_OF_TEXT_cccccc borderWidth:lineH borderType:BorderSideTypeTop];
    
    UIButton *okBtn = [UIToolsControl initButtonWithFrame:CGRectMake(EndX(cancelBtn), StartY(cancelBtn), ViewWidth(cancelBtn), ViewHeight(cancelBtn)) imageName:nil target:self action:@selector(dateEnsureAction) title:@"确定" titleColor:THEMECOLOR_OF_ORANGE];
    okBtn.titleLabel.font = Font(Font_17);
    [bgView addSubview:okBtn];
    [okBtn borderColor:THEMECOLOR_OF_TEXT_cccccc borderWidth:lineH borderType:(BorderSideTypeTop | BorderSideTypeLeft)];
}
- (void)cancelButtonTap {
    if (self.delegate && [self.delegate respondsToSelector:@selector(psWageRangePickerViewCancle)]) {
        [self.delegate psWageRangePickerViewCancle];
    }
}
- (void)dateEnsureAction {
    if (self.delegate && [self.delegate respondsToSelector:@selector(psWageRangePickerView:leftModel:rightModel:)]) {
        PSWageRangeModel *model = self.itemDataArray[leftIndex];
        NSArray *arr = model.itemWageRangeArray;
        PSWageRangeModel *tempModel = arr[rightIndex];
        [self.delegate psWageRangePickerView:self leftModel:model rightModel:tempModel];
    }
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    for (UIView *singleLine in pickerView.subviews) {
        if (singleLine.frame.size.height < 1) {
            singleLine.backgroundColor = THEMECOLOR_OF_TEXT_cccccc;
        }
    }
    UILabel *pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        pickerLabel.textAlignment = NSTextAlignmentCenter;
        pickerLabel.backgroundColor = COLOR_OF_CLEAR;
        pickerLabel.textColor = THEMECOLOR_OF_TEXT_333333;
        [pickerLabel setFont:Font(Font_15)];
    }
    pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return self.itemDataArray.count;
    } else {
        PSWageRangeModel *model = self.itemDataArray[leftIndex];
        NSArray *arr = model.itemWageRangeArray;
        return arr.count;
    }
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (component == 0) {
        PSWageRangeModel *model = self.itemDataArray[row];
        return model.showText;
    } else {
        PSWageRangeModel *model = self.itemDataArray[leftIndex];
        NSArray *arr = model.itemWageRangeArray;
        PSWageRangeModel *tempModel = arr[row];
        return tempModel.showText;
    }
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    return self.frame.size.width / 3;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    if (component == 0) {
        leftIndex = row;
        rightIndex = 0;
        [pickerView reloadComponent:1];
        [pickerView selectRow:0 inComponent:1 animated:YES];
    } else {
        rightIndex = row;
    }
}
- (void)updateWageRangeForMinWage:(NSString *)minWage maxWage:(NSString *)maxWage {
    if (minWage) {
        for (NSInteger i = 0; i < self.itemDataArray.count; i++) {
            PSWageRangeModel *citymodel = self.itemDataArray[i];
            if ([citymodel.dataText isEqualToString:minWage]) {
                leftIndex = i;
                rightIndex = 0;
                [self.pickView reloadAllComponents];
                [self.pickView selectRow:i inComponent:0 animated:YES];
                
                NSArray *arr = citymodel.itemWageRangeArray;
                for (NSInteger j = 0; j < arr.count; j++) {
                    PSWageRangeModel *tempModel = arr[j];
                    if ([tempModel.dataText isEqualToString:maxWage]) {
                        rightIndex = j;
                        [self.pickView selectRow:j inComponent:1 animated:YES];
                        break;
                    }
                }
                break;
            }
        }
    }
}

@end
